package com.example.demo.demos.web.controller;

import com.example.demo.demos.web.entity.Pinpai;
import com.example.demo.demos.web.mapper.PinpaiMapper;
import com.example.demo.demos.web.service.PinpaiService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("index")
public class PinpaiController {
    @Autowired
    private PinpaiMapper pinpaiMapper;
    @Autowired
    private PinpaiService pinpaiService;
    @GetMapping("/pinpai")
    public  Map<String,Object> index(@RequestParam String name,@RequestParam Integer pageNum,@RequestParam Integer pageSize){
pageNum=(pageNum-1)*pageSize;
        Map<String,Object> res=new HashMap<>();
        List<Pinpai> data=pinpaiMapper.select(name,pageNum,pageSize);

        Integer total=pinpaiMapper.totalselect(name);
        res.put("code",200);
        res.put("data",data);
        res.put("total",total);
        return res;
    }
    @PostMapping("/savepp")
    public Map<String,Object> save(@RequestBody Pinpai pinpai){
        Map<String,Object> res=new HashMap<>();
        if(pinpai.getId()==null){
            Integer issacve=pinpaiService.addpinpai(pinpai);
            if(issacve==1){
                res.put("code",200);
                res.put("msg","新增成功");

            }else{
                res.put("code",500);
                res.put("msg","新增失败");

            }
        }else{
            Integer issacve=pinpaiService.editpinpai(pinpai);
            if(issacve==1){
                res.put("code",200);
                res.put("msg","修改成功");

            }else{
                res.put("code",500);
                res.put("msg","修改失败");

            }
        }
        return res;
    }
    @GetMapping("/shanchupp")
    public Map<String,Object> shanchu(@RequestParam String id){
        Map<String,Object> res=new HashMap<>();
        Integer issc=pinpaiMapper.shanchupp(id);
        if(issc == 1){
            res.put("code",200);
            res.put("msg","删除成功");
        }
        return res;
    }
    @GetMapping("/pinpaiall")
    public Map<String,Object> all(){
        Map<String,Object> res=new HashMap<>();
        List<Pinpai> data = pinpaiMapper.selectall();
            res.put("code",200);
            res.put("data",data);
        return res;
    }
}
